package com.changgou.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.bean.User;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Controller
@RequestMapping("/elk")
public class ElkController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @RequestMapping("/createIndex")
    public String createIndex () throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("deasa");
        CreateIndexResponse createIndexResponse =
                restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

        return  createIndexResponse+"";

    }


    @RequestMapping("/getName")
    public String getName () throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest("lvy");


        return  "";
    }

    @RequestMapping("/getIndexb")
    public String getIndex() throws IOException {
        GetIndexRequest getIndexRequest =new GetIndexRequest("lvy");
        boolean exists   = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        if (exists){
            System.out.println(exists);
        }
        return exists+"";
    }

    @RequestMapping("/createIndextest")
    public void createIndextest() throws IOException {
      //  User user = new User("lvy","12","标签");
        IndexRequest indexRequest =new IndexRequest("lvys");
        indexRequest.id("1");
        //数据放入请求
       // indexRequest.source(JSON.toJSON(user));
        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse + "");
    }

    @GetMapping("/searchIndex/{names}")
    public String searchIndex(@PathVariable("names") String names) throws IOException {
        System.out.println(names);
        SearchRequest searchRequest = new SearchRequest("lvy");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
     //   MatchQuery matchQuery = new MatchQuery("","");
      //  TermQueryBuilder termQueryBuilder =new TermQueryBuilder("name","法外狂徒");
        //查询中文需要加一个keyword。
        sourceBuilder.query(QueryBuilders.termQuery("name.keyword",names));
        sourceBuilder.fetchSource(new String[]{"name","tags","age"},new String[]{});
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        return JSON.toJSONString(searchResponse)+"";
    }




}